home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: FreeNet.Carleton.CA!ab598
- From: ab598@FreeNet.Carleton.CA (David Biggar)
- Subject: Re: array of struct pointer switching difficulties
- Message-ID: <DLvLHq.K2x@freenet.carleton.ca>
- Sender: ab598@freenet3.carleton.ca (David Biggar)
- Reply-To: ab598@FreeNet.Carleton.CA (David Biggar)
- Organization: The National Capital FreeNet
- References: <96012622065929517@tglbbs.com>
- Date: Sun, 28 Jan 1996 05:20:13 GMT
-
-
- Charles Herold (charles.herold@tglbbs.com) writes:
- > I posted a question regarding reversing an array of pointers. Two
- > people posted similar answers, but neither works, so perhaps there is a
- > more fundamental problem with my code. Therefore I have put together a
- > full little program that will get the files of a directory, reverse the
- > order they're in, and print them out, so people can see what I've done,
- > and hopefully tell me what I'm doing wrong. In SortFiles, a version of
- > which I posted last time, I have both the reverse sort I use and the one
- > suggested to me. I apologize for posting such a long piece of code, but
- > this seems to be the minimum necessary to show you what the problem is.
- >
- > [some code deleted]
- >
- > struct find_t *files;
- >
- > more code deleted]
- >
- > void SortFiles( struct dir_window *dwindow, int (*compare)(const
- > void *elem1, const void *elem2))
- > {
- > int i, h;
- > struct find_t temp, **files = dwindow->files;
- >
- > /* There's a qsort here I've removed as extraneous */
- > if( reverse_sort ) { /* make it all backwards */
- > #if defined SHOULD_WORK /* told this should work, but doesn't */
- > struct find_t *temp;
- > for (i=0; i < dwindow->numberfiles / 2; i++) {
- > temp = files[i];
- > files[i] = files[dwindow->numberfiles - i - 1];
- > files[dwindow->numberfiles - i - 1] = temp;
- > }
- > #else
- > for( i = 0; i < dwindow->numberfiles / 2; i++ ) {
- > /* now reverse the entire array */
- > temp = (*files)[i];
- > (*files)[i] = (*files)[dwindow->numberfiles - i - 1];
- > (*files)[dwindow->numberfiles - i - 1] = temp;
- > }
- > #endif
- > ................[code deleted]
- >
- > main( int argc, char *argv[] )
- > {
- > if( !(window.numberfiles = GetDirectory( &window, &files) ) ) {
- > fprintf( stderr, "no files found" );
- > return 1;
- > }
- > }
- __________________________________________________________________
-
-
- Assuming your solution is the "#else" part, I believe that's
- correct.
-
- The "SHOULD WORK" part is not correct.
-
- Note that in SortFiles(), the local variable "files" is assigned
- a value of 'ptr to ptr to struct find_t'; the assigned value
- is the same value as '&files', where 'files' is the global 'files'
- whose address was passed to GetDirectory() in main(). In the
- following, I refer to the global 'files' as 'Files'.
-
- So, 'files[i]' in SortFiles() -> *((&Files) + i)
-
- But, the only valid value of 'ptr to struct find_t' is for i = 0;
- for i > 0, values of '*((&Files) + i)'are garbage.
-
- _______________________________________________________________
-
- On the other hand, I believe that you have used an
- unnecessary level of indirection; i.e., from main(), you should
- call GetDirectory() with 'files' rather than '&files'. You
- would have to make corresponding changes in several of the other
- functions but I believe the code would be more readable. The
- change should result in the 'SHOULD WORK' code being correct :-)
-
- _______________________________________________________________
-
-
- Dave
-